//
//  FBDPhotoBrowseViewController.m
//  iOS9Sample-Photos
//
//  Created by feng on 2018/4/7.
//  Copyright © 2018年 小码哥. All rights reserved.
//

#import "FBDPhotoBrowseViewController.h"
#import "FBDPhotoManger.h"
#import "FBDPictureCollectionViewCell.h"
@interface FBDPhotoBrowseViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation FBDPhotoBrowseViewController
{
    UICollectionView*_browseCollection;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    UICollectionViewFlowLayout*flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing=0;
    flowLayout.minimumInteritemSpacing=0;
    flowLayout.itemSize=CGSizeMake([UIScreen mainScreen].bounds.size.width,600);
//    flowLayout.sectionInset=UIEdgeInsetsMake(0,5, 0, 5);
    flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    _browseCollection=[[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    _browseCollection.pagingEnabled=YES;
    _browseCollection.delegate=self;
    _browseCollection.dataSource=self;
    [_browseCollection registerClass:[FBDPictureCollectionViewCell class] forCellWithReuseIdentifier:@"browseCell"];
    [self.view addSubview:_browseCollection];
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [_browseListArray count];
    
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FBDPictureCollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"browseCell" forIndexPath:indexPath];
    
    if (indexPath.row%2) {
        cell.backgroundColor=[UIColor orangeColor];
    }else
    {
        cell.backgroundColor=[UIColor darkTextColor];
        
    }
    
    NSInteger row=indexPath.row;
    PHAsset*asset=[_browseListArray objectAtIndex:row];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
        imageOptions.synchronous = YES;
        
        imageOptions.deliveryMode=PHImageRequestOptionsDeliveryModeOpportunistic;
        imageOptions.resizeMode=PHImageRequestOptionsResizeModeFast;
        imageOptions.networkAccessAllowed=NO;
        imageOptions.progressHandler=^(double progress, NSError *__nullable error, BOOL *stop, NSDictionary *__nullable info)
        {
            NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>%f",progress);
            
        };
        // 图片原尺寸
        CGSize targetSize = CGSizeMake(asset.pixelWidth/2, asset.pixelHeight/2);
        // 请求图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeDefault options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            NSLog(@"图片：%@ /n info :%@", result,info);
            BOOL isiCloudAsset=NO;
            //根据请求会调中的参数重 NSDictionary *info 是否有cloudKey 来判断是否是  iCloud
            if ([[info objectForKey: PHImageResultIsInCloudKey] boolValue])
            {
                isiCloudAsset = YES;
            }
            NSLog(@"isiCloudAsset:>>>>>>%d",isiCloudAsset);
     dispatch_async(dispatch_get_main_queue(), ^{
            cell.middeImageView.image=result;
                        });
            
            
        }];
    });
    
    
    return cell;
}

-(void)setIndexNumber:(NSInteger)indexNumber
{
    _indexNumber=indexNumber;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        _browseCollection.contentOffset=CGPointMake([UIScreen mainScreen].bounds.size.width*_indexNumber, 0);
    });
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
